<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Cert extends Model
{
    use HasFactory;

    protected $fillable = ['title','user_id','remarks','licence','company_id','numbers','type','start_date','end_date','files','level','major','acquired_at','approving_unit','jury_name','date_type','company','aptitude','patent_number','nationality','patent_owner','inventor'];

    const POSTONLY = ['title','user_id','remarks','licence','company_id','numbers','type','start_date','end_date','files','level','major','acquired_at','approving_unit','jury_name','date_type','company','aptitude','patent_number','nationality','patent_owner','inventor'];

    protected $appends = [
        'type_text'
    ];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }
    

    public function getTypeTextAttribute()
    {
        // $arr = [0=>'用户证书',1=>'企业证书'];
        // return $arr[$this->type];
        return $this->type == 1 ? '企业证书' : '用户证书';
    }

    public function users()
    {
        return $this->belongsToMany(User::class,'cert_users');
    }

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name','idcard');
    }

    public function downs()
    {
        return $this->hasMany(CertDown::class,'cert_id','id')->with('user')->with('application');
    }

    public function application()
    {
        return $this->hasOne(Application::class,'source_id','id')->with('project')->with('user')->where('model','Cert')->orderBy('id','desc');
    }

    
}
